package com.lbyl.activitiy;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.lbyl.fragment.BlogFragment;
import com.lbyl.fragment.MsgFragment;
import com.lbyl.fragment.MyFragment;
import com.lbyl.fragment.TopNewsFragment;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    private Fragment[] fragments=new Fragment[4];
    private Context mContext;
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.mContext = this;

        initFragment();


    }

    /**
     * 初始化四个页面
     */
    private void initFragment() {
        fragments[0]=new TopNewsFragment();
        fragments[1]=new BlogFragment();
        fragments[2]=new MsgFragment();
        fragments[3]=new MyFragment();


        getSupportFragmentManager().beginTransaction().add(R.id.flayout_main, fragments[0]).add(R.id.flayout_main, fragments[1]).add(R.id.flayout_main, fragments[2]).add(R.id.flayout_main, fragments[3]).commit();
    }

    public void onclick_topnews(View view) {
        Log.i(TAG, "onclick_topnews: ");
        getSupportFragmentManager().beginTransaction().hide(fragments[1]).hide(fragments[2]).hide(fragments[3]).show(fragments[0]).commit();
        setActionBarTitle("头条");
    }
    public void onclick_blog(View view) {
        Log.i(TAG, "onclick_blog: ");
        getSupportFragmentManager().beginTransaction().hide(fragments[0]).hide(fragments[2]).hide(fragments[3]).show(fragments[1]).commit();
        setActionBarTitle("博客");
    }
    public void onclick_msg(View view) {
        Log.i(TAG, "onclick_msg: ");
        getSupportFragmentManager().beginTransaction().hide(fragments[0]).hide(fragments[1]).hide(fragments[3]).show(fragments[2]).commit();
        setActionBarTitle("消息");
    }
    public void onclick_my(View view) {
        Log.i(TAG, "onclick_my: ");
        getSupportFragmentManager().beginTransaction().hide(fragments[0]).hide(fragments[1]).hide(fragments[2]).show(fragments[3]).commit();
        Log.i(TAG, "onclick_topnews: 即将设置title");
        setActionBarTitle("我的");
    }
    public void setActionBarTitle(String title){
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar == null) {
            Log.i(TAG, "setActionBarTitle: actionBar空指针");
            return;
        }
        supportActionBar.setTitle(title);

    }


}
